// *****************************************************
// Aitrox Information Technology
// http://www.proxima-ai.com
// Copyright 2021 Aitrox. All rights reserved.
//
// Filename: download.go
// CreateDate: 2023-06-07
// Author: zjp
// Description: 下载相关数据库操作
// ******************************************************
package model

import (
	"github.com/jinzhu/gorm"
)

type Download struct {
	*Model
	OrganType     int    `json:"organType" gorm:"Column:organType"`
	FileName      string `json:"fileName" gorm:"Column:fileName"`
	CreateTime    string `json:"createTime" gorm:"Column:createTime" sql:"-"`
	DownloadTimes int    `json:"downloadTimes" gorm:"Column:downloadTimes"`
}

type WaitDownloadCount struct {
	OrganType         int `json:"organType" gorm:"Column:organType"`
	WaitDownloadTimes int `json:"waitDownloadTimes" gorm:"Column:waitDownloadTimes"`
}

type DownloadTimes struct {
	DownloadTimes int `json:"downloadTimes" gorm:"Column:downloadTimes"`
}

type DownloadFileList struct {
	List []*Download
}

type DownloadFileListSwagger struct {
	*SwaggerSuccess
	Data *DownloadFileList `json:"data"`
}

type DownloadTimesSwagger struct {
	*SwaggerSuccess
	Data *DownloadTimes `json:"data"`
}

func (d Download) TableName() string {
	return "t_download_file_info"
}

/**
 * @brief 通过检查部位获取待下载文件总数
 * @param db-数据库访问实例
 * @param organType-检查部位
 * @return 用户ID，错误信息
 */
func GetWaitDownloadCount(db *gorm.DB, organType int) (int, error) {
	var d Download = Download{OrganType: organType}
	count := 0
	db = db.Table(d.TableName()).Where("organType = ? and downloadTimes = 0 and isDelete = 0", d.OrganType)
	err := db.Count(&count).Error

	if err != nil && err != gorm.ErrRecordNotFound {
		return 0, err
	}

	return count, nil
}

/**
 * @brief 获取待下载列表总数
 * @param db-数据库访问实例
 * @return 待下载列表总数，错误信息
 */
func GetWaitDownloadTypeCount(db *gorm.DB) (res []*WaitDownloadCount, err error) {

	var d Download = Download{}
	db = db.Table(d.TableName()).Select("organType, count(downloadTimes) as waitDownloadTimes").Where("downloadTimes = 0 and isDelete = 0").Group("organType")
	ret := db.Find(&res)
	if ret.Error != nil {
		return res, ret.Error
	}
	return res, nil
}

/**
 * @brief 通过检查部位获取下载文件信息
 * @param db-数据库访问实例
 * @param organType-检查部位
 * @return 检查文件信息列表，错误信息
 */
func List(db *gorm.DB, organType int) ([]*Download, error) {
	var d Download = Download{OrganType: organType}
	var l DownloadFileList
	res := db.Table(d.TableName()).Select("id, organType, fileName, downloadTimes, createTime").Where("organType = ? and isDelete = 0", d.OrganType).Order("createTime DESC").Find(&l.List)
	if res.Error != nil {
		return nil, res.Error
	}
	return l.List, nil
}

/**
 * @brief 插入数据
 * @param db-数据库访问实例
 * @return 错误信息
 */
func (info Download) InsertDownloadFile(db *gorm.DB) error {
	res := db.Table(info.TableName()).Create(&info)
	if res.Error != nil {
		return res.Error
	}
	return nil
}

/**
 * @brief 更新下载次数
 * @param db-数据库访问实例
 * @return 错误信息
 */
func (info Download) UpdateDownloadTimes(db *gorm.DB) error {
	res := db.Exec("update t_download_file_info set downloadTimes = downloadTimes + 1 where id = ? and isDelete = 0", info.ID)
	return res.Error
}

/**
 * @brief 删除下载文件信息
 * @param db-数据库访问实例
 * @param organType-检查部位
 * @param fileNames-文件名集合
 * @return 影响条数，错误信息
 */
func DeleteDownloadFiles(db *gorm.DB, organType int, fileNames []string) (int, error) {
	var info Download
	res := db.Table(info.TableName()).Where("fileName in (?)", fileNames).Where("organType = ?", organType).Delete(&info)
	return int(res.RowsAffected), res.Error
}
